var express = require('express')
var path = require('path')
var formidable = require('formidable')

var router = express.Router()

//ajax文件上传
router.post('/uploads', function (req, res) {
  //创建表单解析对象
  var form = new formidable.IncomingForm();
  //设置存放路径
  form.uploadDir = path.join(__dirname, '../', 'public', 'uploads');
  //保存后缀名
  form.keepExtensions = true;
  // 解析客户端传递过来的formdata对象
  form.parse(req, function (err, fields, files) {
    res.json({
      //将路径发送给前端
      path: files.attrName.path.split('public')[1]
    })
  })
})

module.exports = router;
